//
//  DecryptOrderDetailVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/22.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "DecryptOrderDetailVC.h"
#import "DecryptOrderModel.h"
#import "HCSStarRatingView.h"
#import "CancleOrderVC.h"
#import "ReportOrderVC.h"
#import "Masonry.h"
#import "EndServiceInsuredVC.h"
#import "EvaluateInsuredVC.h"
#import "EaseMessageViewController.h"
#import "RefuseOrderVC.h"
#import "WorkmateMainVC.h"
#import "HelpViewController.h"
#import "PreviewResumeVC.h"
#import "OtherPeopleVC.h"

@interface DecryptOrderDetailVC ()
@property (weak, nonatomic) IBOutlet UIView *topView;


@property (weak, nonatomic) IBOutlet UILabel *typeTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;

@property (weak, nonatomic) IBOutlet UILabel *jobNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *userHeadImageView;
@property (weak, nonatomic) IBOutlet UILabel *userDetailLabel;


@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *meesageLabel1;
@property (weak, nonatomic) IBOutlet UIButton *desMoreButton;


@property (weak, nonatomic) IBOutlet UIView *cancelTitleView;
@property (weak, nonatomic) IBOutlet UILabel *cancelTitleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cancelTitleViewHeight;
@property (weak, nonatomic) IBOutlet UIView *cancelView;
@property (weak, nonatomic) IBOutlet UILabel *cancelDesLabel;
@property (weak, nonatomic) IBOutlet UIButton *cancelMoreButton;


@property (weak, nonatomic) IBOutlet UILabel *orderNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderTimeLabel;


@property (weak, nonatomic) IBOutlet UIView *refuseView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *refuseViewHeight;
@property (weak, nonatomic) IBOutlet UIView *refuseContentView;
@property (weak, nonatomic) IBOutlet UIImageView *refuseUserHeadView;
@property (weak, nonatomic) IBOutlet UILabel *refuseContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *refuseBackLabel;
@property (weak, nonatomic) IBOutlet UIButton *explainButton;




@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *chatButton;




@property (weak, nonatomic) IBOutlet UIView *evaluateBottomView;
@property (weak, nonatomic) IBOutlet UIButton *endEvaluateButton;//已结束已评价的查看聊天按钮


@property(nonatomic,strong) DecryptOrderModel *model;


@end

@implementation DecryptOrderDetailVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"订单详情";
    self.topView.layer.borderColor = UIColorFromRGB(0X26BFCE).CGColor;
    self.topView.layer.borderWidth = 1;
    self.explainButton.layer.borderColor = UIColorFromRGB(0X00C6D5).CGColor;
    self.explainButton.layer.borderWidth = 1;
    
    
    [self rightButton];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestDecrypt) name:@"reloadBuyDecryptOrder" object:nil];
    [self requestDecrypt];
}
-(void)requestDecrypt{
    __weak typeof(self) weakSelf = self;
    [self.view bringSubviewToFront:self.loadingView];
    self.loadingView.hidden = NO;
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"decryptId":self.relatedId} url:@"decrypt/getDecryptOrder" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.view sendSubviewToBack:self.loadingView];
            self.loadingView.hidden = YES;
            weakSelf.model = [DecryptOrderModel mj_objectWithKeyValues:result[@"data"]];
            [weakSelf loadViewData];
            
        }else{
            
            
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
}

-(void)rightButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    //    [backBtn setTitle:@"保存" forState:UIControlStateNormal];
    [backBtn setImage:IMAGE(@"ic_collect_help") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(help) forControlEvents:UIControlEventTouchUpInside];
    //    backBtn.backgroundColor = kGreenColor;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
    }
}
-(void)help{
    HelpViewController *vc = [[HelpViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)loadViewData{
    
    self.explainButton.hidden = YES;
    self.bottomView.hidden = YES;
    self.evaluateBottomView.hidden = YES;
    
    self.typeTitleLabel.text = [NSString stringWithFormat:@"%@-",self.model.decryptStatusName];
    
    if ([self.model.decryptStatus isEqualToString:@"TO_BE_CONFIRMED"]) {
        /**待确认*/
        /**购买者*/
        self.typeLabel.text = @"待同事确认";
        [self hiddenResuseView];
        [self hiddenCancelView];
//        self.cancelButton.hidden = NO;
        self.bottomView.hidden = NO;
    }else if ([self.model.decryptStatus isEqualToString:@"DUE_TO_PAY"]) {
        /**待支付*/
        self.typeLabel.text = @"待同事支付";
        [self hiddenResuseView];
        [self hiddenCancelView];
        
        
    }else if ([self.model.decryptStatus isEqualToString:@"ONGOING"]) {
        /**进行中*/
        self.typeLabel.text = [NSString stringWithFormat:@"%@关闭服务",self.model.closeTime];
        self.chatButton.hidden = NO;
        self.bottomView.hidden = NO;
        [self hiddenResuseView];
        [self hiddenCancelView];
        
        
    }else if ([self.model.decryptStatus isEqualToString:@"REFUSE"]) {
        /**已拒绝*/
        [self hiddenResuseView];
        self.bottomView.hidden = YES;
        
        self.typeLabel.text = @"已拒绝订单";
        self.cancelDesLabel.text = self.model.rejectRemark;
        self.cancelTitleLabel.text = @"拒绝描述";
        
    }else if ([self.model.decryptStatus isEqualToString:@"FINISHED"]) {
        /**已结束*/
        self.typeLabel.text = [NSString stringWithFormat:@"已结束服务"];
        self.evaluateBottomView.hidden = NO;
        if ([self.model.subscribeEvaluted isEqualToString:@"0"]) {
            //订购人是否评论,未评论
            
            
            [self hiddenResuseView];
            [self hiddenCancelView];
        }else{
            [self hiddenCancelView];
            if ([self.model.publisherEvaluted isEqualToString:@"0"]) {
                //在职者是否评论,未评论
                self.explainButton.hidden = NO;
                
                [self hiddenCancelView];
            }else{
                
                [self hiddenCancelView];
                
            }
        }
        
        
        
    }else if ([self.model.decryptStatus isEqualToString:@"CANCELLED"]) {
        /**取消*/
        self.bottomView.hidden = YES;
        self.typeLabel.text = @"已取消订单";
        [self hiddenResuseView];
        self.cancelDesLabel.text = self.model.cancelRemark;
        self.cancelTitleLabel.text = @"取消描述";
    }
    
    
    
    self.jobNameLabel.text = self.model.title;
    self.userNameLabel.text = [NSString stringWithFormat:@"台阶用户：%@",self.model.purchaser];
    [self.userHeadImageView sd_setImageWithURL:[NSURL URLWithString:ImageURL(self.model.purchaserHeadImg)] placeholderImage:IMAGE(@"默认图")];
    self.userDetailLabel.text = [NSString stringWithFormat:@"已购解密%zd次 | 好评率%.0f%@",self.model.purchaseCount,self.model.purchaserDegreeOfSatisfaction*100,@"%"];;
    
    self.contentLabel.text = self.model.question;
    self.meesageLabel1.text = self.model.personalSituation;
    if (self.model.orderNo.length > 0) {
        self.orderNumLabel.text = [NSString stringWithFormat:@"订单号:%@",self.model.orderNo];
    }else{
        self.orderNumLabel.text = [NSString stringWithFormat:@"订单号"];
    }
    
    self.orderTimeLabel.text = [NSString stringWithFormat:@"提交时间:%@",self.model.submitTime];
    
    
    
    
    
    self.refuseContentLabel.text = self.model.subscribeEvalutedContent;
    if(self.model.publisherEvalutedContent.length == 0){
        self.refuseBackLabel.text = @" ";
    }else{
        self.refuseBackLabel.text = [NSString stringWithFormat:@"同事回复：%@",self.model.publisherEvalutedContent];
    }
    [self.refuseUserHeadView sd_setImageWithURL:[NSURL URLWithString:ImageURL(self.model.purchaserHeadImg)] placeholderImage:IMAGE(@"默认图")];
    
    
    
}
/*
 方法描述:
 隐藏取消原因
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)hiddenCancelView{
    self.cancelView.frame = CGRectMake(self.cancelView.mj_x, self.cancelView.mj_y, self.cancelView.mj_w, 0);
    self.cancelView.hidden = YES;
    [self.cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(0);
    }];
    self.cancelTitleView.hidden = YES;
    self.cancelTitleViewHeight.constant = 0;
}
/*
 方法描述:
 隐藏拒绝原因
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)hiddenResuseView{
    self.refuseContentView.frame = CGRectMake(self.cancelView.mj_x, self.cancelView.mj_y, self.cancelView.mj_w, 0);
    self.refuseContentView.hidden = YES;
    [self.refuseContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(0);
    }];
    self.refuseView.hidden = YES;
    self.refuseViewHeight.constant = 0;
}
- (IBAction)userDetailClick:(id)sender {
    OtherPeopleVC *vc = [[OtherPeopleVC alloc]init];
    vc.user_id = [NSString stringWithFormat:@"%zd",self.model.purchaserId];
    [self.navigationController pushViewController:vc animated:YES];
//    WorkmateMainVC *vc = [[WorkmateMainVC alloc] init];
//    vc.userId = [NSString stringWithFormat:@"%zd",self.model.purchaserId];
//    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)desMoreButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.meesageLabel1.numberOfLines = 0;
    }else{
        self.meesageLabel1.numberOfLines = 3;
    }
}
- (IBAction)cancelMoreButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.cancelDesLabel.numberOfLines = 0;
    }else{
        self.cancelDesLabel.numberOfLines = 3;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)refuseClick:(id )sender {
    
    RefuseOrderVC *vc = [[RefuseOrderVC alloc]init];
    vc.relatedId = self.relatedId;
    vc.type = 1;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)acceptClick:(id)sender {
    NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                          self.relatedId,@"decryptId",
                                          nil];
    
    
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:dicInfoParams url:@"decrypt/accept" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            
            [self requestDecrypt];
            
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
}
- (IBAction)payOrderClick:(id)sender {
    [self showProgressWithView:self.view animated:YES];
    
    [ [CommonUtils sharedCommonUtils] payWithType:@"GOODS_TYPE_JM" relatedId:self.relatedId result:^(NSDictionary *result, BOOL paySuccess) {
        [self hideProgress:self.view animated:YES];
        if (paySuccess) {
            ALERT_VIEW(@"支付成功");
            [self requestDecrypt];
        }else{
//            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}
- (IBAction)chatClick:(id)sender {
      [CommonUtils gotoChatViewControllerWithUserName:self.model.purchaser user_id:[NSString stringWithFormat:@"%zd",self.model.purchaserId] vc:self];
   
}
- (IBAction)endServiceClick:(id)sender {
    NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                          [NSString stringWithFormat:@"%@",self.relatedId],@"decryptId",
                                          nil];
    
    
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:dicInfoParams url:@"decrypt/finish" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self requestDecrypt];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
}
- (IBAction)evaluateClick:(id)sender {
    EvaluateInsuredVC *vc =[[EvaluateInsuredVC alloc]init];
    vc.relatedId = self.relatedId;
    vc.type = 1;
    vc.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:vc animated:YES completion:^{}];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
